////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
//  This software for customer relationship management system, developed by Iflytek-ZY-GCZX team.
//  Software code and design for the team, copy rights reserved.
////////////////////////////////////////////////////////////////////////////////

package com.iflytek.ci.common.exceptions;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * <p>
 * 平台捕捉到的异常，用于将CheckedException转换为UncheckedException，以便不让代码捕捉一些不必要的异常，
 * 并且使用此异常在堆栈信息打印时不会产生多余的堆栈信息。
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2012-11-18 12:29 PM
 * @since JDK 1.5
 */
public class UncheckedException extends BaseRuntimeException {

	private static final long serialVersionUID = -1194365408621642496L;

	public UncheckedException(Throwable cause) {
		super("依赖第三方功能异常", cause, "ERR-UN-000", getCauseException(cause));
	}

	public UncheckedException(String message, Throwable cause) {
		super(message, cause, "ERR-UN-000", getCauseException(cause));
	}

	/**
	 * 得到原始异常信息
	 *
	 * @param cause
	 * @return
	 */
	public static String getCauseException(Throwable cause) {
		if (cause != null) {
			cause = ExceptionSupporter.getRootCause(cause);
			return cause.getClass().getName() + ":" + cause.getMessage();
		} else {
			return "Cause Exception is null";
		}
	}

	@Override
	public void printStackTrace(PrintStream s) {
		s.print(getMessage());
		s.println(", The Exception is: ");
		super.getCause().printStackTrace(s);
	}

	@Override
	public void printStackTrace(PrintWriter s) {
		s.print(getMessage());
		s.println(", The Exception is: ");
		super.getCause().printStackTrace(s);
	}
}
